package Game;

/**
 * RandomEvents.java
 * 
 * This class represents the random events generator that calculates the chance of a random event 
 * happening during the player's turn
 * 
 * 
 * @author Ray DeCuir
 * @version 1.0
 *
 */

// READ THIS BEFORE USING
/*
 * 
 * call playerCaravan = calculateEvents(playerCaravan) to return the new Caravan
 * Then get the event type and message to the user given in the getLastEvent() and getMessage() methods
 * 
 * 
 */


public class RandomEvents {

	
	// probabilities of different events happening
	private final double CHANCEOFMEXICANATTACK = 0.04;
	private final double CHANCEOFTHEFT = 0.04;
	private final double CHANCEOFMEXICANSGIVEYOUFOOD = 0.06;
	private final double CHANCEOFVEHICLEDAMAGE = 0.08;
	private final double CHANCEOFFOODPOISONING = 0.04;
	private final double CHANCEOFSTORM = 0.08;
	
	// other multipliers used to calculate the chance of a random event
	private final double CHARACTERMULTI = 0.1;
	private double rationMulti, paceMulti;
	
	// messages that are used to update the gui and inform the player that a random event has occurred
	private String lastEvent;
	private String message;
	
	/**
	 * This method determines if a random event will occur, and if so which event will take place
	 * 
	 * @param playerCaravan The player's Caravan
	 * @param playerRation The players ration
	 * @param playerPace The players ration
	 * @return The player's updated caravan, after any random event changes
	 */
	public Caravan calculateEvents( Caravan playerCaravan, Ration playerRation, Pace playerPace)
	{
		this.rationMulti = playerRation.getMulti();
		this.paceMulti = playerPace.getMulti();
		lastEvent = null;
		message = null;
		
		if ( Math.random() < CHANCEOFMEXICANATTACK)
			return attack(playerCaravan);
		if ( Math.random() < CHANCEOFTHEFT)
			return theft(playerCaravan);
		if ( Math.random() < CHANCEOFMEXICANSGIVEYOUFOOD)
			return giveFood(playerCaravan);
		if ( Math.random() < CHANCEOFVEHICLEDAMAGE)
			return vehicleDamage(playerCaravan);
		if ( Math.random() < CHANCEOFFOODPOISONING)
			return foodPoisoning(playerCaravan);
		if ( Math.random() < CHANCEOFSTORM)
			return storm(playerCaravan);
		return playerCaravan;
	}
	
	/**
	 * This private method determines if an attack random event has taken place, and updates the player's caravan if one has
	 * 
	 * @return The player's caravan after being updated by the possible random event
	 */
	private Caravan attack(Caravan playerCaravan)
	{
		double chanceOfSuccess = 0.5 - 0.1 * ( paceMulti - 0.7 ) - CHARACTERMULTI * playerCaravan.countCharacterType(CharacterType.WRESTLER);
		Character c = playerCaravan.getRandomCharacter();
		if ( Math.random() < chanceOfSuccess && c != null){
			lastEvent = "attack";
			c.setHealthStatus(HealthState.DEAD);
			message = "You were attacked by wandering locals! " + c.getName() + " was killed in the brawl!";
			System.out.println(message);
			
		}
		return playerCaravan;
	}
	
	
	/**
	 * This private method determines if a theft random event has taken place, and updates the player's caravan if one has
	 * 
	 * @return The player's caravan after being updated by the possible random event
	 */
	private Caravan theft(Caravan playerCaravan)
	{
		double chanceOfSuccess = 0.5 - 0.1 * ( paceMulti - 0.7 ) - CHARACTERMULTI * playerCaravan.countCharacterType(CharacterType.WRESTLER);
		if ( Math.random() < chanceOfSuccess){

			if ( playerCaravan.getPlayerInventory().getNumberOfItems() > 0){		
				lastEvent = "theft";
				int id = playerCaravan.getPlayerInventory().getRandomInventoryItemId();
				message = "You were attacked by wandering locals! They stole a " + GameDriver.getItemList().getItemName(id) + "!";
				playerCaravan.getPlayerInventory().addItem(id, -1);
				System.out.println(message);
			}
		}
		return playerCaravan;
	}
	
	
	/**
	 * This private method determines if a give food random event has taken place, and updates the player's caravan if one has
	 * 
	 * @return The player's caravan after being updated by the possible random event
	 */
	private Caravan giveFood(Caravan playerCaravan)
	{
		double chanceOfSuccess = 0.5 - 0.1 * ( paceMulti ) ;
		if ( Math.random() < chanceOfSuccess){
			lastEvent = "giveFood";
			message = "You met some locals while traveling. They gave you 15 burritos!";
			int id = GameDriver.getItemList().getIdOf("name", "Burrito");
			playerCaravan.getPlayerInventory().addItem(id, 15);
			System.out.println(message);
		}
		return playerCaravan;
	}
	
	
	/**
	 * This private method determines if a vehicle damage random event has taken place, and updates the player's caravan if one has
	 * 
	 * @return The player's caravan after being updated by the possible random event
	 */
	private Caravan vehicleDamage(Caravan playerCaravan)
	{
		double chanceOfSuccess = 0.5 + 0.1 * ( paceMulti  ) - 3 * CHARACTERMULTI * playerCaravan.countCharacterType(CharacterType.MECHANIC);
		if ( Math.random() < chanceOfSuccess){
			lastEvent = "vehicleDamage";
			playerCaravan.getVehicles().get(0).breakRandomVehiclePart();
			message = "Your " + GameDriver.getItemList().getItemName(playerCaravan.getVehicles().get(0).findBrokenPart()) + " has broken while traveling!";
			System.out.println(message);
		}
		return playerCaravan;
	}
	
	
	/**
	 * This private method determines if a food poisoning random event has taken place, and updates the player's caravan if one has
	 * 
	 * @return The player's caravan after being updated by the possible random event
	 */
	private Caravan foodPoisoning(Caravan playerCaravan)
	{
		double chanceOfSuccess = 0.5 - 0.1 * ( rationMulti - 0.7 ) - CHARACTERMULTI * playerCaravan.countCharacterType(CharacterType.JANITOR);
		Character c = playerCaravan.getRandomCharacter();
		if ( Math.random() < chanceOfSuccess && c != null){
			lastEvent = "foodPoisoning";
			c.setHealthStatus(HealthState.DEAD);
			message = c.getName() + " suffered from Food poisoning and died!";	
			System.out.println(message);
		}
		return playerCaravan;
	}
	
	
	/**
	 * This private method determines if a storm random event has taken place, and updates the player's caravan if one has
	 * 
	 * @return The player's caravan after being updated by the possible random event
	 */
	private Caravan storm(Caravan playerCaravan)
	{
		double chanceOfSuccess = 0.5;
		if ( Math.random() < chanceOfSuccess && playerCaravan.getPlayerInventory().getNumberOfItems() > 0){
			lastEvent = "storm";
			int id = playerCaravan.getPlayerInventory().getRandomInventoryItemId();
			playerCaravan.getPlayerInventory().addItem(id, -1);
			message = "A devastating storm swept over you path! Your " + GameDriver.getItemList().getItemName(id) + " was destroyed!";
			System.out.println(message);
		}
		return playerCaravan;
	}
	
	
	/**
	 * Gets a string representation of the last random event
	 * 
	 * @return A string representation of the last random event
	 */
	public String getLastEvent()
	{
		return lastEvent;
	}
	
	
	/**
	 * Gets a string that informs the user what has happened in the last random event
	 * 
	 * @return A string representation of what happened in the last random event
	 */
	public String getMessage()
	{
		return message;
	}

}
